package com.itlong.cloud.controller.padbot;

import com.itlong.cloud.iottp.IPadBotRobotServiceClient;
import com.itlong.cloud.project.property.IPropertyDeliveryTaskServiceClient;
import com.itlong.cloud.service.RedisService;
import com.itlong.cloud.thrown.DataAccessException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <desc>
 *      接收派宝回调信息控制器
 * </desc>
 *
 * @createDate2021/1/6
 **/
@RestController
@RequestMapping("padbot")
public class PadBotCallbackController {

    public static final Logger logger = LoggerFactory.getLogger(PadBotCallbackController.class);

    @Autowired
    private RedisService redisService;

    @Autowired
    private IPadBotRobotServiceClient iPadBotRobotServiceClient;

    @Autowired
    private IPropertyDeliveryTaskServiceClient iPropertyDeliveryTaskServiceClient;
    /**
     * <desc>
     *      接收派送任务状态回调接口
     * </desc>
     *
     * @param
     * @return
     * @Author Zhu.yj
     * @createDate  2021/1/6
     */
    @RequestMapping(value = "receiveDeliveryTaskStatus", method = RequestMethod.POST)
    public Object receiveDeliveryTaskStatus(String taskId, String status){
        try{
            Map<String, Object> resultCode = new HashMap<>();
            resultCode.put("messageCode", 10000);

            boolean flag = iPropertyDeliveryTaskServiceClient.updateTaskStatus(taskId, status);

           return resultCode;

        }catch (Exception e){
            throw new DataAccessException("【派宝回调控制器receiveDeliveryTaskStatus】接收派送任务状态回调失败", e);
        }
    }


    /**
     * <desc>
     *      接收地图更新回调地址
     * </desc>
     *
     * @param serialNumber 机器人序列号
     * @return
     * @Author Zhu.yj
     * @createDate  2021/1/8
     */
    @RequestMapping(value = "receiveMapUpdate", method = RequestMethod.POST)
    public Object receiveMapUpdate(String serialNumber){
        try{
            Map<String, Object> resultCode = new HashMap<>();
            resultCode.put("messageCode", 10000);
            if (StringUtils.isNotBlank(serialNumber)){
                iPadBotRobotServiceClient.saveMapTargetInfo(serialNumber);
            }
            return resultCode;
        }catch (Exception e){
            throw new DataAccessException("【派宝回调控制器receiveDeliveryTaskStatus】接收派送任务状态回调失败", e);
        }
    }

    /**
     * <desc>
     *      接收派送任务信息回调
     * </desc>
     *
     * @param  taskJson 回调信息
     * @return
     * @Author Zhu.yj
     * @createDate  2021/1/13
     */
    @PostMapping("receiveDeliveryTask")
    public Object receiveDeliveryTask(String taskJson){
        try{
            Map<String, Object> resultCode = new HashMap<>();
            logger.info("【派送任务回调内容】" + taskJson);
            resultCode.put("messageCode", 10000);
            if (StringUtils.isNotBlank(taskJson)){
                iPropertyDeliveryTaskServiceClient.receiveDeliveryTaskInfo(taskJson);
            }
            return resultCode;
        }catch (Exception e){
            throw new DataAccessException("【派宝回调控制器receiveDeliveryTask】接收派送任务信息回调失败", e);
        }
    }
}
